package com.jieyang.oa.controller.myInformation;

import com.jieyang.oa.controller.BaseServlet;
import com.jieyang.oa.pojo.Vo.EmpInfoVo;
import com.jieyang.oa.pojo.entity.Employee;
import com.jieyang.oa.service.EmployeeService;
import com.jieyang.oa.service.MyInfService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @author 29276
 * @version 1.0
 * @description: 我的信息
 * @date 2021/8/28 7:39
 */
@WebServlet("/myInfServlet")
public class MyInfServlet extends BaseServlet {

    @Autowired
    private EmployeeService employeeService;

    /**
     * @Author 2927624517@qq.com
     * @Description 更新我的信息
     * @Date 2021/8/28 15:10
     * @Param [req, resp]
     * @return void
    **/
    public void updateMyInf(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");

        String phone = req.getParameter("phone");
        String qq = req.getParameter("qq");
        String emercontactPerson = req.getParameter("emercontactPerson");

        Employee emp = (Employee) req.getSession().getAttribute("emp");
        String empId = emp.getEmpId();

        Employee employee = new Employee();
        employee.setEmpId(empId);
        employee.setPhone(phone);
        employee.setEmail(qq);
        employee.setEmercontactPerson(emercontactPerson);
        if (phone != null && !phone.equals("")) {
            emp.setPhone(phone);
        }
        if (qq != null && !qq.equals("")) {
            emp.setEmail(qq);
        }
        if (emercontactPerson != null && !emercontactPerson.equals("")) {
            emp.setEmercontactPerson(emercontactPerson);
        }

        Integer update = employeeService.update(emp);
        if (update == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 修改我的密码
     * @Date 2021/8/28 15:10
     * @Param [req, resp]
     * @return void
    **/
    public void updatePwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");
        String oldPassword = req.getParameter("oldPassword");
        String newPassword = req.getParameter("newPassword");
        String newPasswordSure = req.getParameter("newPasswordSure");
        Employee emp = (Employee) req.getSession().getAttribute("emp");
        if (newPassword.length()<8) {
            resp.getWriter().write("4");
        }
        else if (!oldPassword.equals(emp.getPassword())){
            resp.getWriter().write("2");
        }
        else if (!newPassword.equals(newPasswordSure)) {
            resp.getWriter().write("3");
        }
        else if (oldPassword.equals(emp.getPassword())) {
            emp.setPassword(newPassword);
            Integer update = employeeService.update(emp);
            if (update == 0) {
                resp.getWriter().write("0");
            } else {
                resp.getWriter().write("1");
            }
        }
        else {
            resp.getWriter().write("0");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查询我的信息
     * @Date 2021/8/28 15:10
     * @Param [req, resp]
     * @return void
    **/
    public void selectMy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        resp.setContentType("text/html;charset=utf-8");
        Employee emp = (Employee) req.getSession().getAttribute("emp");
        String empId = emp.getEmpId();

        EmpInfoVo employee = employeeService.selectByEmpId(empId);

        req.setAttribute("emp", employee);
        req.getRequestDispatcher("myInformation/myInfo.jsp").forward(req, resp);

    }
}
